package com.sx.pipeline.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.gson.Gson
import com.noober.background.view.BLTextView
import com.sx.pipeline.R
import com.sx.pipeline.app.config.ReleaseTaskType
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.repos.http.res.ReleaseTaskVo
import com.sx.pipeline.ui.task.entity.ReleaseTaskListVO

/**
 * @Author：YY
 * @Date：2022/2/10
 * @Describe: 任务列表适配器
 */
class ReleaseTaskListAdapter(
) : BaseQuickAdapter<ReleaseTaskVo, BaseViewHolder>(
    R.layout.item_operating_unit_release
) {
    /**
     * 取消发布
     */
    var cancelPostCallback: ((Int) -> Unit)? = null

    /**
     * 结束任务
     */
    var endTaskCallback: ((Int) -> Unit)? = null

    /**
     * 删除任务
     */
    var deleteTaskCallback: ((Int) -> Unit)? = null
    override fun convert(holder: BaseViewHolder, item: ReleaseTaskVo) {
        //工作性质
        holder.setText(R.id.tv_nature,item.nature)
        //任务时间
        holder.setText(R.id.tv_start_time,item.startTime)
        //任务标题
        holder.setText(R.id.tv_title,item.title)
        //项目名称
        holder.setText(R.id.tv_project_name,"项目名称："+item.name)
        //工作量
        holder.setText(R.id.tv_work_load,"工作量："+item.workload+item.unit)
        //施工地点
        holder.setText(R.id.tv_release_address,"施工地点："+item.addressDetail)
        //现场负责人
        holder.setText(R.id.tv_release_principal,"现场负责人："+item.contact)

        //任务状态
        when(item.status){
            ReleaseTaskType.PENDING_REVIEW.code->holder.setText(R.id.tv_status,R.string.txt_pending_review)
            ReleaseTaskType.AGENCY.code->holder.setText(R.id.tv_status,R.string.txt_upcoming)
            ReleaseTaskType.IN_PROGRESS.code->holder.setText(R.id.tv_status,R.string.txt_in_progress)
            ReleaseTaskType.COMPLETED.code->holder.setText(R.id.tv_status,R.string.txt_completed)
            ReleaseTaskType.OVER.code->holder.setText(R.id.tv_status,R.string.txt_over)
            ReleaseTaskType.REJECTED.code->holder.setText(R.id.tv_status,R.string.txt_rejected)
            ReleaseTaskType.CANCELLED.code->holder.setText(R.id.tv_status,R.string.txt_cancelled)
        }
        when (item.status) {
            //待审核
            ReleaseTaskType.PENDING_REVIEW.code -> {
                holder.setText(R.id.tv_action, context.getString(R.string.txt_cancel_release))
                holder.setGone(R.id.tv_delete, true)
                holder.setVisible(R.id.tv_action, true)
            }
            //待办
            ReleaseTaskType.AGENCY.code -> {
                holder.setText(R.id.tv_action, context.getString(R.string.txt_end_task))
                holder.setGone(R.id.tv_delete, true)
                holder.setVisible(R.id.tv_action, true)
            }
            //在办
            ReleaseTaskType.IN_PROGRESS.code -> {
                holder.setText(R.id.tv_action, context.getString(R.string.txt_apply_extension))
                holder.setGone(R.id.tv_delete, true)
                holder.setVisible(R.id.tv_action, true)
            }
            //待评价
//            3 -> {
//                holder.setText(R.id.tv_action, context.getString(R.string.txt_immediately_evaluate))
//                holder.setGone(R.id.tv_delete, true)
//                holder.setVisible(R.id.tv_action, true)
//            }
            //已完成
            ReleaseTaskType.COMPLETED.code -> {
                holder.setText(R.id.tv_delete, context.getString(R.string.txt_delete_task))
                if (!item.evaluate){
                    holder.setText(R.id.tv_action, context.getString(R.string.txt_immediately_evaluate))
                }else{
                    holder.setText(R.id.tv_action, context.getString(R.string.txt_view_reviews))
                }
                holder.setVisible(R.id.tv_delete, true)
                holder.setVisible(R.id.tv_action, true)


            }
            //已结束
            ReleaseTaskType.OVER.code -> {
                holder.setText(R.id.tv_delete, context.getString(R.string.txt_delete_task))
                holder.setVisible(R.id.tv_delete, true)
                holder.setGone(R.id.tv_action, true)
            }
            ReleaseTaskType.REJECTED.code->{
               holder.setText(R.id.tv_delete, context.getString(R.string.txt_delete_task))
               holder.setText(R.id.tv_action, context.getString(R.string.txt_rejection_reason))
                holder.setVisible(R.id.tv_delete, true)
                holder.setVisible(R.id.tv_action, true)
            }
            //已取消
            ReleaseTaskType.CANCELLED.code -> {
                holder.setText(R.id.tv_delete, context.getString(R.string.txt_delete_task))
                holder.setVisible(R.id.tv_delete, true)
                holder.setGone(R.id.tv_action, true)
            }
        }

        holder.getView<BLTextView>(R.id.tv_action).setOnClickListener {
            when (item.status) {
                //取消任务
                ReleaseTaskType.PENDING_REVIEW.code -> cancelPostCallback?.invoke(item.id)
                //结束任务
                ReleaseTaskType.AGENCY.code -> endTaskCallback?.invoke(item.id)
                //申请延期
                ReleaseTaskType.IN_PROGRESS.code -> RouteManager.Mine.intentToTaskPostpone(item.id)
                ReleaseTaskType.COMPLETED.code->{
                    if (!item.evaluate){
                        RouteManager.Mine.intentToEvaluationNow(Gson().toJson(item))
                    }else{
                        RouteManager.Mine.intentToEvaluationList(item.id)
                    }
                }
                //已驳回 跳转详情查看拒绝原因
                ReleaseTaskType.REJECTED.code -> RouteManager.Mine.intentToReleaseTaskDetail(item.id)
            }
        }
        holder.getView<BLTextView>(R.id.tv_delete).setOnClickListener {
            when (item.status) {
                //删除任务
                ReleaseTaskType.COMPLETED.code,
                ReleaseTaskType.OVER.code,
                ReleaseTaskType.REJECTED.code,
                ReleaseTaskType.CANCELLED.code -> deleteTaskCallback?.invoke(item.id)
            }
        }
    }

}